package com.zhudi.utils;

import com.zhudi.commen.BaseResponse;
import com.zhudi.commen.ErrorCode;

/**
 * 这个类用来生成一些固定的错误
 */
public class ResultUtils {

    //todo 现在error用的好像很少，思考一下应该怎么改写
    public static <T> BaseResponse<T> Error(ErrorCode errorCode, String description){
        return new BaseResponse<T>(errorCode.getCode(), null, errorCode.getMessage(),description);
    }
    public static BaseResponse<?> Error(int code, String message, String description) {
        return new BaseResponse<>(code, null,message,description);
    }
    public static <T> BaseResponse<T> success(T data, String description){
        return new BaseResponse<T>(ErrorCode.SUCCESS.getCode(),data,ErrorCode.SUCCESS.getMessage(),description);
    }

    public static <T> BaseResponse<Boolean> result(boolean success){
        if(success){
            return   success(success,"执行成功");
        }else {
          return   Error(ErrorCode.USER_ERROR,"执行失败");
        }
    }


    public static <T> BaseResponse<T> result(T result,String description){
        if(result!=null){
            return   success(result,description);
        }else {
            return   Error(ErrorCode.USER_ERROR,description);
        }
    }
    public static <T> BaseResponse<T> result(T result,boolean success){
        if(success){
            return   success(result,"执行成功");
        }else {
            return   Error(ErrorCode.USER_ERROR,"执行失败");
        }
    }

}
